package com.hpeng.basic.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hpeng.basic.service.CategoryManager;
import com.hpeng.sys.model.Category;
import com.hpeng.util.Constant;
import com.hpeng.util.Pagination;
import com.opensymphony.xwork2.ActionSupport;

@Component("categoryAction")
@Scope("prototype")
public class CategoryAction extends ActionSupport{
	@Resource
	private CategoryManager categoryManager;
	private List<Category> categoryList;
	private Pagination<Category> pagination;
	private String catid;	//根据类型Id删除

	private Category category;	//资产类型对象
	private String originalCategoryId;	//要被修改的categoryId
	//分页部分
	private int page;	//当前页数
	private final String uri = "searchCategory.action";
	public String pageLink;
	//根据条件查询部门
	private String conditionValue;	//条件值
	//错误处理
	private String msg;
	
//找出所有资产类型
	public String findCategory(){
		categoryList = categoryManager.findCategory();
		return "success";
	}
	
//根据条件查询
	public String queryCategory(){
		String append = " where catname like ('%"+conditionValue+"%') " +
				"or catsimplename like ('%"+conditionValue+"%')" +
						" or catalias like ('%"+conditionValue+"%')" +//名称与别名模糊查询
							" or catid like ('"+conditionValue+"%')";//Id模糊查询
		//查出catid位数为10的所有类别
		String[] hql = {" from Category",append};
		String order = " order by catid asc";
		setPagination(categoryManager.getPagination(Constant.PAGE_SIZE, page, hql, order));
		pageLink = pagination.getPageLink(uri);
		return "success";
	}
	
//分页查询所有资产类型
	public String searchCategory(){
System.out.println("分页查询: ");
		String[] hql = {" from Category"};
		String order = " order by catid asc";
		setPagination(categoryManager.getPagination(Constant.PAGE_SIZE, page, hql, order));
System.out.println("pagination.getDataList().size()"+pagination.getDataList().size());
		pageLink = pagination.getPageLink(uri);
		return "success";
	}
	
//通用删除方法,可根据单个条件删除,可根据数组删除.
	public String deleteCategory(){
		String[] stringArray = catid.split(",");
		
		stringArray = categoryManager.isLastLevelCategory(stringArray);	//检查是否是最末级节点
		stringArray = categoryManager.getNoUse(stringArray);	//检查是否被资产卡片表使用
		if(stringArray.length == 0){
			msg = getText("delcat.failure");
		}else{
			try{
				categoryManager.deleteAllCategory(stringArray);
			}catch(RuntimeException e){
				msg = getText("del.failure");
			}if (msg == null || "".equals(msg.trim())) {
				msg = getText("del.success");
			}
		}
		return searchCategory();
	}

//根据Id传递信息
	public String passValue(){
		category = categoryManager.findCategoryById(catid);
		return "success";
	}
//更新物品类别
	public String updateCategory(){

		if("".equals(originalCategoryId)){
			//不改变分类Id
			categoryManager.updateCategory(category);
		}else{
			//改变分类的Id为category.getCatid()下的分类
			//检查是否是末级节点并且没有被资产引用
			String[] temp = {originalCategoryId};
			temp = categoryManager.isLastLevelCategory(temp);
			temp = categoryManager.getNoUse(temp);
			if(temp.length != 0){
				categoryManager.deleteAllCategory(originalCategoryId);
				categoryManager.autoAddCategory(category);
			}else{
				msg = getText("updcat.failure");
			}
		}
		return "success";
	}

	
//添加
	public String addCategory(){
		try{
			category.setCatactualstock("0");
			categoryManager.autoAddCategory(category);
		} catch (RuntimeException e) {
			msg = getText("save.failure");
		}if (msg == null || "".equals(msg.trim())) {
			msg = getText("save.success");
		}
		return searchCategory();
	}

	/*	setter,getter构造器	*/

	public void setPagination(Pagination<Category> pagination) {
		this.pagination = pagination;
	}
	
	public Pagination<Category> getPagination() {
		return pagination;
	}

	public void setCategoryManager(CategoryManager categoryManager) {
		this.categoryManager = categoryManager;
	}

	public List<Category> getCategoryList() {
		return categoryList;
	}
	
	public void setPage(int page){
		this.page = page;
	}
	public int getPage() {
		return page;
	}

	public String getCatid() {
		return catid;
	}

	public void setCatid(String catid) {
		this.catid = catid;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public Category getCategory() {
		return category;
	}

	public void setConditionValue(String conditionValue) {
		this.conditionValue = conditionValue;
	}

	public String getConditionValue() {
		return conditionValue;
	}

	public void setOriginalCategoryId(String originalCategoryId) {
		this.originalCategoryId = originalCategoryId;
	}

	public String getOriginalCategoryId() {
		return originalCategoryId;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getMsg() {
		return msg;
	}
}
